<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Cmdlog;
use app\admin\model\Roomclean;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;

class DeviceService
{

    public function __construct()
    {

        import('.Apis.Aep_device_command', EXTEND_PATH, '.php');

    }

    /**
     * 电信IOT
     */
    public function dxIotTime($req){


        $deviceModel = new Shopdevice();
        $d = $deviceModel->where('device_sn',$req['device_sn'])->find();
        $productId = 0;
        $maskKey = "";
        if($d['is_san'] == 1){
            $productId = 17019253;
            $maskKey = "9c30b3ec7cdd4578a28a40b777337239";
        }else if($d['is_san'] == 2){
            $productId = 17017655;
            $maskKey = "7a2bfc3eb1ec409cb246c1110195a4f5";
        }

        $content = array();
        $cmd = array();
        $params = array();
        $params['countdown']= $req['sec'];
        $params['time']= $req['time'];
        $cmd['params'] = $params;
        $cmd['serviceIdentifier'] = "countdown_cmd";
        $content['deviceId'] = $req['device_sn'];
        $content['operator'] = "wangxifeng13647312688";
        $content['productId'] = $productId;
        $content['ttl'] = 0;
        $content['content'] = $cmd;

        $result = \Aep_device_command::CreateCommand("dcnAAtMoIje","5gKnAiOrJQ", $maskKey,json_encode($content));
        error_log($req['device_sn']."====result===".json_encode($result));

        return $result;
    }


    public function dxIotDoor($req){



        $deviceModel = new Shopdevice();
        $d = $deviceModel->where('device_sn',$req['device_sn'])->find();
        $productId = 0;

        $maskKey = "";
        if($d['is_san'] == 1){
            $productId = 17019253;
            $maskKey = "9c30b3ec7cdd4578a28a40b777337239";
        }else if($d['is_san'] == 2){
            $productId = 17017655;
            $maskKey = "7a2bfc3eb1ec409cb246c1110195a4f5";
        }
        $content = array();
        $cmd = array();
        $params = array();
        $params['onoff1']= $req['open'];
        $params['onoff2']= $req['open2'];
        $params['onoff3']= $req['open3'];
        $params['onoff4']= $req['open4'];
        $cmd['params'] = $params;
        $cmd['serviceIdentifier'] = "onoff_cmd";
        $content['deviceId'] = $req['device_sn'];
        $content['operator'] = "wangxifeng13647312688";
        $content['productId'] = $productId;
        $content['ttl'] = 0;
        $content['content'] = $cmd;

        $result = \Aep_device_command::CreateCommand("dcnAAtMoIje","5gKnAiOrJQ", $maskKey,json_encode($content));
        error_log($req['device_sn']."====result===".json_encode($result));
        return $result;
    }


    public function openRoom($book, $user_id,$openCmdUrl)
    {

        $deviceModel = new Shopdevice();
        $roomBookModel = new Shoproombook();
        $device = $deviceModel->where('shop_id', $book['shop_id'])->where('device_type','qp')->where('room_id', $book['room_id'])->find();
        $orderModel = new Shoporder();
        $order = $orderModel->where('id', $book['order_id'])->find();
        $orderNo = $order['order_no'];
        $minute = floor(($book['end_time'] - time()) % 86400 / 60);
        $sn = $device['sn'];
        if ($device) {


            if($device['version'] == 1){
                $req = array();
                $req['sec'] = $minute * 60;
                $req['orderNo'] = $orderNo;
                $req['sn'] = $sn;
                $req['voice'] = "";

                $ret = http_post_json($openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            }else{

                $req  = array();
                $req['sec'] = $minute*60;
                $req['orderId'] = $order['id'];
                $req['deviceSn'] = $device['device_sn'];
                $ret = http_post_json($openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            }





            $shopRoomModel = new Shoproom();
            $shopRoomModel->where('id',$book['room_id'])->update(['status'=>'busy']);

            $roomBookModel->where('id', $book['id'])->update(['status' => 'doing']);
            $orderModel->where('id', $book['order_id'])->update(['status' => 'doing']);





            $cmdModel = new Cmdlog();
            $cmdModel->insert(['user_id'=>$user_id,'cmd'=>json_encode($req),'create_time'=>time(),'shop_id'=>$book['shop_id'],'room_id'=>$book['room_id']]);

        }
    }


    public function closeRoom($order,$user_id,$openCmdUrl)
    {

        $deviceModel = new Shopdevice();
        $roomBookModel = new Shoproombook();
        $device = $deviceModel->where('shop_id', $order['shop_id'])->where('room_id', $order['room_id'])->find();
        $orderModel = new Shoporder();
        $orderNo = $order['order_no'];
        $sn = $device['sn'];
        if ($device) {

            $req = array();
            $req['sec'] = 0;
            $req['orderNo'] = $orderNo;
            $req['sn'] = $sn;
            $req['voice'] = "";



            $roomBookModel->where('id', $order['id'])->update(['status' => 'done']);
            $orderModel->where('id', $order['id'])->update(['status' => 'done']);

            $ret = http_post_json($openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }

            $cmdModel = new Cmdlog();
            $cmdModel->insert(['user_id'=>$user_id,'cmd'=>json_encode($req),'create_time'=>time(),'shop_id'=>$order['shop_id'],'room_id'=>$order['room_id']]);

        }
        //error_log("====cmd ret===" . json_encode($ret));
    }



    public function xuRoom($book, $user_id,$order_no,$openCmdUrl)
    {

        $deviceModel = new Shopdevice();
        $roomBookModel = new Shoproombook();
        $device = $deviceModel->where('shop_id', $book['shop_id'])->where('room_id', $book['room_id'])->find();
        $orderModel = new Shoporder();
        $minute = floor(($book['end_time'] - time()) % 86400 / 60);
        $sn = $device['sn'];
        if ($device) {

            $req = array();
            $req['sec'] = $minute * 60;
            $req['orderNo'] = $order_no;
            $req['sn'] = $sn;
            $req['voice'] = "";


            $roomBookModel->where('id', $book['id'])->update(['status' => 'doing']);
            $orderModel->where('id', $book['order_id'])->update(['status' => 'doing']);

            $ret = http_post_json($openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }

            $cmdModel = new Cmdlog();
            $cmdModel->insert(['user_id'=>$user_id,'cmd'=>json_encode($req),'create_time'=>time(),'shop_id'=>$book['shop_id'],'room_id'=>$book['room_id']]);


        }
        error_log("====cmd ret===" . json_encode($ret));
    }

}